import {Page} from './page.js';
function updateUser() {
    let url="user.Servlet?param=addUser";
    alert("update");
    window.location.href = url + "&uid=" + row.firstElementChild.innerHTML;
}

function deleteUser(eventTarget) {
    let url="user.Servlet?param=deleteUser";
    let flag=confirm("do you want to delete this row?");
    if(flag){
        let row=eventTarget.parentNode.parentNode.parentNode;
        let num=row.firstElementChild.innerHTML;
        console.log(num);
        row.parentNode.removeChild(row);
        window.location.href = url + "&uid=" + row.firstElementChild.innerHTML;
    }
}

function option(event) {
    let value = "";
    let id = 0;
    let eventTarget = event.target;
    if (eventTarget.tagName.toLowerCase() == "span") {
        value = eventTarget.innerHTML;
    } else if (eventTarget.tagName.toLowerCase() == "div") {
        value = eventTarget.nextElementSibling.innerHTML;
    }
    id = eventTarget.parentNode.dataset.id;
    switch (value) {
        case "删除":
            deleteUser(eventTarget);
            break;
        case "修改":
            updateUser();
            break;
    }
}

/*function exitInput(event){

    let text=event.target.value;
    event.target.parentNode.innerHTML=text;
}*/
/*function inputClick(event){
    let td=event.target;
    if(td.tagName.toLowerCase()=='td'&& ! td.dataset.flag){
        let input=document.createElement("input");
        input.type="text";
        input.value=td.innerHTML;
        td.innerHTML="";
        td.appendChild(input);
        input.focus();
        input.addEventListener("blur",exitInput,false);
    }
}*/
function addTd(){
    let url="user.Servlet?param=add&array=";
    let myTbody=document.getElementById("myTbody");
    let trsLength=myTbody.rows.length;
    let row=myTbody.insertRow(trsLength);
    let myTd=document.createElement("td");
    row.appendChild(myTd);
    let text=document.createTextNode("**点击添加后自动获得id**");
    myTd.appendChild(text);

    for(let i=0;i<3;i++){
        myTd=document.createElement("td");
        row.appendChild(myTd);
        let myInput=document.createElement("input");
        myTd.appendChild(myInput);
    }
    myTd=document.createElement("td");
    row.appendChild(myTd);
    let newDiv=document.createElement("div");
    newDiv.innerHTML=`<div class="td_div">
                                <input type="raDio" name="sex" id="manUser" value="1" class="normalRadio">
                                <label for="manUser" class="checkText text">男</label>
                                <input type="radio" name="sex" id="womanUser" value="2" class="normalRadio">
                                <label for="manUser" class="checkText text">女</label>
                            </div>`;
    myTd.appendChild(newDiv);
    for(let i=0;i<2;i++){
        myTd=document.createElement("td");
        row.appendChild(myTd);
        let myInput=document.createElement("input");
        myTd.appendChild(myInput);
    }
    let tdx=document.createElement("td");
    let savadiv=document.createElement("div");
    savadiv.classList.add("divButton");
    savadiv.classList.add("saveButton");
    savadiv.addEventListener("click",event=>{
        let currentTr=event.target.parentNode.parentNode;
        let arrays=currentTr.getElementsByTagName("input");
        let newarrays=new Array();
        let i=0;
        [...arrays].forEach(item=>{
            if (item.name=="sex"&&!item.checked){
            }else{
                let text=item.value;
                newarrays[i++]=text;
            }

            //item.parentNode.innerHTML=text;
        });
        window.location.href = url+newarrays;
       /* newarrays.forEach(item=>{
            console.log(newarrays[--i]);
        });*/


        //row.removeChild(tdx);
    },false);
    row.appendChild(tdx);
    tdx.appendChild(savadiv);
}

function toLastPageToAdd(){
    let url="user.Servlet?param=table&addFlag=add&currentPage=";
    let currentPage = document.getElementById("totalPageNumber").innerText;
    window.location.href = url+currentPage;
}

function init(){
    let page=new Page();
    let url="user.Servlet?param=table";
    let extra=document.getElementById("btn_addTd").value;
    if ("add"==extra) addTd();

    document.getElementById("myTbody").addEventListener("click", option, false);
    //document.getElementById("myTbody").addEventListener("dblclick",inputClick,false);
    document.getElementById("btn_addTd").addEventListener("click",toLastPageToAdd,false)
    document.getElementById("pageGroup").addEventListener("click",event=>page.pageOpration(event,url));
    document.getElementById("jumpPage").addEventListener("change",event=>page.pageOpration(event,url));
}

window.addEventListener("load",init,false);